import { describe } from 'mocha'
import sinon from 'sinon'
import * as fs from 'fs'
import * as util from '../src/util'
import { exec } from 'child_process';
import { PackInputs } from '../src/pack-inputs'

const mkdirSafe = (path: string) => {
  try {
    fs.mkdirSync(path, {'recursive': true})
  } catch(e) {
  }
}

const rmdirSafe = (path: string) => {
  try {
    fs.rmdirSync(path, {'recursive': true})
  } catch(e) {
  }
}

describe('Tar file', () => {
  beforeEach((done) => {
    fs.writeFile('/tmp/a.txt', '', (err) => {
      // if (err) throw err;
      mkdirSafe('/tmp/a')
      mkdirSafe('/tmp/b') 
      mkdirSafe('/tmp/c') 
      mkdirSafe('/tmp/folder') 
      done()
    });
  })
  afterEach((done) => {
    fs.unlink('/tmp/a.txt', (err) => {
      // if (err) throw err;
      rmdirSafe('/tmp/a')
      rmdirSafe('/tmp/b')
      rmdirSafe('/tmp/c')
      rmdirSafe('/tmp/folder')
      done()
    });    
  })

  describe('On AIX', () => {
    let stubExec: any
    let packInputs: PackInputs

    beforeEach(() => {
      stubExec = sinon.spy()
      packInputs = {} as PackInputs
      util.setPlatform('aix')
      util.setExecutor(stubExec)
    })
    afterEach(() => {
      util.setPlatform('linux')
      util.setExecutor(exec)
    })
    
    it('should pack single file without path', () => {
      packInputs.filePath = '/tmp/a.txt'
      packInputs.sourcePath = ['/tmp/a.txt']
      packInputs.includePathInArtifact = 'false'
      packInputs.artifactFullName = 'package.tgz'
      util.tarPack(packInputs)
      sinon.assert.callCount(stubExec, 1)
      sinon.assert.calledWith(stubExec, 'tar  -cf - -C /tmp a.txt | gzip -c > package.tgz')
    })

    it('should pack single file with path', () => {
      packInputs.filePath = '/tmp/a.txt'
      packInputs.sourcePath = ['/tmp/a.txt']
      packInputs.includePathInArtifact = 'true'
      packInputs.artifactFullName = 'package.tgz'
      util.tarPack(packInputs)
      sinon.assert.callCount(stubExec, 1)
      sinon.assert.calledWith(stubExec, 'tar  -cf - /tmp/a.txt | gzip -c > package.tgz')
    })

    it('should pack single dir without path', () => {
      packInputs.filePath = '/tmp/folder'
      packInputs.sourcePath = ['/tmp/folder']
      packInputs.includePathInArtifact = 'false'
      packInputs.artifactFullName = 'package.tgz'
      util.tarPack(packInputs)
      sinon.assert.callCount(stubExec, 1)
      sinon.assert.calledWith(stubExec, 'tar  -cf - -C /tmp/folder . | gzip -c > package.tgz')
    })

    it('should pack single dir end with back slash without path', () => {
      packInputs.filePath = '/tmp/folder/'
      packInputs.sourcePath = ['/tmp/folder/']
      packInputs.includePathInArtifact = 'false'
      packInputs.artifactFullName = 'package.tgz'
      util.tarPack(packInputs)
      sinon.assert.callCount(stubExec, 1)
      sinon.assert.calledWith(stubExec, 'tar  -cf - -C /tmp/folder/ . | gzip -c > package.tgz')
    })

    it('should pack single dir with path', () => {
      packInputs.filePath = '/tmp/folder'
      packInputs.sourcePath = ['/tmp/folder']
      packInputs.includePathInArtifact = 'true'
      packInputs.artifactFullName = 'package.tgz'
      util.tarPack(packInputs)
      sinon.assert.callCount(stubExec, 1)
      sinon.assert.calledWith(stubExec, 'tar  -cf - /tmp/folder | gzip -c > package.tgz')
    })

    it('should pack multi file with path', () => {
      packInputs.filePath = '/tmp/a.txt /tmp/b.txt /tmp/c.txt'
      packInputs.sourcePath = ['/tmp/a.txt', '/tmp/b.txt', '/tmp/c.txt']
      packInputs.includePathInArtifact = 'true'
      packInputs.artifactFullName = 'package.tgz'
      util.tarPack(packInputs)
      sinon.assert.callCount(stubExec, 1)
      sinon.assert.calledWith(stubExec, 'tar  -cf - /tmp/a.txt /tmp/b.txt /tmp/c.txt | gzip -c > package.tgz')
    })

    it('should pack multi dir with path', () => {
      packInputs.filePath = '/tmp/a /tmp/b /tmp/c'
      packInputs.sourcePath = ['/tmp/a', '/tmp/b', '/tmp/c']
      packInputs.includePathInArtifact = 'true'
      packInputs.artifactFullName = 'package.tgz'
      util.tarPack(packInputs)
      sinon.assert.callCount(stubExec, 1)
      sinon.assert.calledWith(stubExec, 'tar  -cf - /tmp/a /tmp/b /tmp/c | gzip -c > package.tgz')
    })

    it('should pack multi file with path even includePathInArtifact is false', () => {
      packInputs.filePath = '/tmp/a.txt /tmp/b.txt /tmp/c.txt'
      packInputs.sourcePath = ['/tmp/a.txt', '/tmp/b.txt', '/tmp/c.txt']
      packInputs.includePathInArtifact = 'false'
      packInputs.artifactFullName = 'package.tgz'
      util.tarPack(packInputs)
      sinon.assert.callCount(stubExec, 1)
      sinon.assert.calledWith(stubExec, 'tar  -cf - /tmp/a.txt /tmp/b.txt /tmp/c.txt | gzip -c > package.tgz')
    })
  })
})
